<?php

namespace D3hdv\Diablo3Bundle\Entity;
use \Symfony\Component\Security\Core\User\UserInterface;
use Doctrine\Common\Collections\ArrayCollection;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 */
class D3Compte implements UserInterface
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    protected $id;
    
    /**
     * @ORM\Column(type="string", length=255)
     */
    protected $login;   


    
    /**
     * @ORM\Column(type="string", length=255)
     */
    protected $password;   
 
    /**
     * @var string
     *
     * @ORM\Column(name="email", type="string", length=255)
     */
    protected $email;

   
    /**
     * @ORM\Column(type="string", length=255)
     *
     * @var string salt
     */
    protected $salt;

   /**
   * @ORM\OneToOne(targetEntity="D3hdv\Diablo3Bundle\Entity\D3Coffre", cascade={"persist"})
   */
  protected $Coffre;
 
    /**
     * @ORM\ManyToMany(targetEntity="Role")
     * @ORM\JoinTable(name="_user_role",
     *     joinColumns={@ORM\JoinColumn(name="compte_id", referencedColumnName="id")},
     *     inverseJoinColumns={@ORM\JoinColumn(name="role_id", referencedColumnName="id")}
     * )
     *
     * @var ArrayCollection $userRoles
     */
    protected $userRoles;    
    

    public function __construct()
    {
        $this->createdAt = new \DateTime();
        $this->userRoles = new ArrayCollection();
        $this->updatedAt = new \DateTime();
    }    
    
    /**
     * Gets an array of roles.
     * 
     * @return array An array of Role objects
     */
    public function getRoles()
    {
        return $this->getUserRoles()->toArray();
    }
    
    /**
     * Gets the user roles.
     *
     * @return ArrayCollection A Doctrine ArrayCollection
     */
    public function getUserRoles()
    {
        return $this->userRoles;
    }
 
    /**
     * Compares this user to another to determine if they are the same.
     * 
     * @param UserInterface $user The user
     * @return boolean True if equal, false othwerwise.
     */
    public function equals(UserInterface $user)
    {
        return md5($this->getUsername()) == md5($user->getUsername());
    }
    
    
    /**
     * Gets the user password.
     *
     * @return string The password.
     */
    public function getPassword()
    {
        return $this->password;
    }
 
    /**
     * Sets the user password.
     *
     * @param string $value The password.
     */
    public function setPassword($value)
    {
        $this->password = $value;
    }
 
    /**
     * Gets the user salt.
     *
     * @return string The salt.
     */
    public function getSalt()
    {
        return $this->salt;
    }
 
    /**
     * Sets the user salt.
     *
     * @param string $value The salt.
     */
    public function setSalt($value)
    {
        $this->salt = $value;
    }
    
    /**
     * Gets the username.
     *
     * @return string The username.
     */
    public function getUsername()
    {
        return $this->login;
    }
 
    /**
     * Sets the username.
     *
     * @param string $value The username.
     */
    public function setUsername($value)
    {
        $this->login = $value;
    }    
    
    /**
     * Erases the user credentials.
     */
    public function eraseCredentials()
    {
 
    }   

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Add userRoles
     *
     * @param \D3hdv\Diablo3Bundle\Entity\Role $userRoles
     * @return D3Compte
     */
    public function addUserRole(\D3hdv\Diablo3Bundle\Entity\Role $userRoles)
    {
        $this->userRoles[] = $userRoles;
    
        return $this;
    }

    /**
     * Remove userRoles
     *
     * @param \D3hdv\Diablo3Bundle\Entity\Role $userRoles
     */
    public function removeUserRole(\D3hdv\Diablo3Bundle\Entity\Role $userRoles)
    {
        $this->userRoles->removeElement($userRoles);
    }

    /**
     * Set email
     *
     * @param string $email
     * @return D3Compte
     */
    public function setEmail($email)
    {
        $this->email = $email;
    
        return $this;
    }

    /**
     * Get email
     *
     * @return string 
     */
    public function getEmail()
    {
        return $this->email;
    }

    /**
     * Set Coffre
     *
     * @param \D3hdv\Diablo3Bundle\Entity\D3Coffre $coffre
     * @return D3Compte
     */
    public function setCoffre(\D3hdv\Diablo3Bundle\Entity\D3Coffre $coffre = null)
    {
        $this->Coffre = $coffre;
    
        return $this;
    }

    /**
     * Get Coffre
     *
     * @return \D3hdv\Diablo3Bundle\Entity\D3Coffre 
     */
    public function getCoffre()
    {
        return $this->Coffre;
    }

    public function serialize()
	{
		return serialize(array(
		$this->id,
		$this->password,
		$this->username,
		$this->email,
		$this->salt,
		$this->Coffre,
		$this->userRoles
		));
	}

	public function unserialize($serialized)
	{
		list(
		$this->id,
		$this->password,
		$this->username,
		$this->email,
		$this->salt,
		$this->Coffre,
		$this->userRoles
		) = unserialize($serialized);
	}
   public function __sleep()
    {
        return array('id');
    }
}
